package com.gome.ocean.service.task;

import java.util.List;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gome.ocean.common.enums.DBSwitchType;
import com.gome.ocean.common.enums.DataSourceStatus;
import com.gome.ocean.common.utils.SpringContextUtil;
import com.gome.ocean.dao.model.gddl.AppBO;
import com.gome.ocean.dao.model.gddl.AppConfigBO;
import com.gome.ocean.dao.model.gddl.AppDatasourceBO;
import com.gome.ocean.dao.model.gddl.GroupBO;
import com.gome.ocean.dao.model.gddl.GroupRuleBO;
import com.gome.ocean.service.gddl.AppConfigSerivce;
import com.gome.ocean.service.gddl.AppDatasourceService;
import com.gome.ocean.service.gddl.AppService;
import com.gome.ocean.service.gddl.GroupRuleService;
import com.gome.ocean.service.gddl.GroupService;


public class MasterSlaveAutoSwitchJobTask extends ScheduledJobTask {

    private static final Logger  LOG = LoggerFactory.getLogger(MasterSlaveAutoSwitchJobTask.class);

    private AppService           appService;
    private AppConfigSerivce     appConfigSerivce;
    private AppDatasourceService appDatasourceService;
    private GroupService         groupService;
    private GroupRuleService     groupRuleService;

    @Override
    public boolean checkTask() throws JobExecutionException {
        return true;
    }

    @Override
    public void initTask() throws JobExecutionException {
        if (this.appService == null) this.appService = SpringContextUtil.getBean("appService");
        if (this.appConfigSerivce == null) this.appConfigSerivce = SpringContextUtil.getBean("appConfigSerivce");
        if (this.groupService == null) this.groupService = SpringContextUtil.getBean("groupService");
        if (this.groupRuleService == null) this.groupRuleService = SpringContextUtil.getBean("groupRuleService");
        if (this.appDatasourceService == null)
            this.appDatasourceService = SpringContextUtil.getBean("appDatasourceService");
    }

    @Override
    public void runTask() throws JobExecutionException {
        List<AppBO> Apps = appService.findAllApps();
        for (AppBO appBO : Apps) {
            AppConfigBO appConfigBO = appConfigSerivce.findAppConfigBOByAppId(appBO.getId());
            if (appConfigBO == null) continue;
            if (DBSwitchType.NOT_AUTO == DBSwitchType.getDBSwitchType(appConfigBO.getIsAutoSwitch())) {
                continue;
            }
            List<GroupBO> groupBOs = groupService.findGroupBOByAppId(appBO.getId());
            for (GroupBO groupBO : groupBOs) {
                List<GroupRuleBO> groupRuleBOs = groupRuleService.findGroupRuleBOByGroupId(groupBO.getId());
                if (groupRuleBOs.size() == 1) {
                    LOG.info("app db is greater than two db! please check it! appname : "
                             + appBO.getAppName());
                    continue;
                }
                for (GroupRuleBO groupRuleBO : groupRuleBOs) {
                    AppDatasourceBO appDatasourceBO = appDatasourceService
                        .findAppDatasourceBOById(groupRuleBO.getDatasourceId());
                    if (appDatasourceBO != null) {
                        DataSourceStatus dataSourceStatus = DataSourceStatus
                            .getDataSourceStatus(appDatasourceBO.getSourceStatus());
                        if (DataSourceStatus.ERROR == dataSourceStatus) {
                            try {
                                Thread.sleep(appConfigBO.getSwitchDelayTime() * 1000);
                            } catch (InterruptedException e) {
                            }
                            groupRuleService.switchBackUpDb(groupBO, groupRuleBOs);
                            break;
                        }
                    }
                }
            }
        }
        LOG.info("complete auto switch db task!");
    }
}
